/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action;

import com.dao.AdminDao;
import com.dao.TeacherDao;
import com.dao.Usermanager;
import com.entity.Admin;
import com.entity.Student;
import com.entity.Teacher;
import com.opensymphony.xwork2.ActionSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author DELL
 */
public class Login extends ActionSupport {

    private String userName;
    private String sno;
    private Integer id;
    private String password;
    private Integer radiobutton;
    private HttpServletRequest request;

    private String checkCode;
    Usermanager smUsermanager = new Usermanager();

    public String getUserName() {

        return this.userName;
    }

    public void setUserName(String userName) {

        this.userName = userName;
    }

    public String getSno() {

        return this.sno;
    }

    public void setSno(String sno) {

        this.sno = sno;
    }

    public Integer getId() {

        return this.id;
    }

    public void setId(Integer id) {

        this.id = id;
    }

    public String getPassword() {

        return this.password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public Integer getRadiobutton() {

        return this.radiobutton;
    }

    public void setRadiobutton(Integer radiobutton) {

        this.radiobutton = radiobutton;
    }

    public String getCheckCode() {
        return checkCode;
    }

    public void setCheckCode(String checkCode) {
        this.checkCode = checkCode;
    }

    @Override
    public void validate() {
        
                request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();

        String checkCode2 = (String) session.getAttribute("checkCode");

        if (!checkCode.equals(checkCode2)) {
            this.addFieldError("checkCode", "输入的验证码不正确,请重新输入!");
        }

        String regex = "\\p{Alnum}{4,16}";

        if (!getPassword().matches(regex)) {
            
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, getPassword());
            
            this.addFieldError("password","密码只能由4~16位数字或字母组成！");
            
          
        }
        
        
        

    }

    
    
    
    
    
    
    
    public String execute() throws Exception {

        request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();

        if (radiobutton == 1) {
            Student s = new Student();

            s.setSno(getSno());
            s.setPassword(getPassword());

            Logger.getLogger(this.getClass().getName()).log(Level.INFO, s.getSno());
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, s.getPassword());

            if (smUsermanager.stuLogin(s)) {
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "shuchutest!daffffffffffff");

                Student sn = smUsermanager.getStudent1(sno);
                session.setAttribute("username", sn.getUserName());
                session.setAttribute("sno", this.sno);
                session.setAttribute("id", sn.getStId());

                Student st = smUsermanager.getStudent1(sno);
                session.setAttribute("student", st);

                return SUCCESS;
            }
            return INPUT;

        } else if (radiobutton == 2) {

            TeacherDao td = new TeacherDao();
            Teacher tea = new Teacher();

            tea.setTeaId(getSno());
            tea.setTpassword(getPassword());

            Logger.getLogger(this.getClass().getName()).log(Level.INFO, tea.getTeaId());
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, tea.getTpassword());

            if (td.tcLogin(tea)) {
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "shuchutest!daffffffffffff");

                Teacher teacher = td.getTeacher1(sno);
                session.setAttribute("tname", teacher.getTname());

                session.setAttribute("tid", teacher.getTid());
                return "tsuccess";
            }
            return INPUT;

        } else if (radiobutton == 3) {

            AdminDao ad = new AdminDao();
            Admin am = new Admin();
            am.setUsername(getSno());
            am.setPassword(getPassword());

            if (ad.adminLogin(am)) {

                Admin admin = ad.getAdmin(sno);
                session.setAttribute("username", admin.getUsername());

                session.setAttribute("id", admin.getId());
                return "asuccess";
            }
            return INPUT;

        } else {

            return INPUT;
        }

    }

}
